using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Windows.Forms;
using ArcEngine.Graphics;
using ArcEngine.Resources;
using System.Collections;
using WeifenLuo.WinFormsUI.Docking;
using System.Xml;
using System.Text;


namespace ArcEngine.Editor
{
    public partial class MainForm : Form
    {


		/// <summary>
		/// Constructor
		/// </summary>
		public MainForm()
		{
			// Form initialize
			InitializeComponent();


			Config.Load();

			// Log panel
			logPanel = new LogForm();
			logPanel.Show(dockPanel, DockState.DockBottomAutoHide);

			// Resource panel
			ResourcePanel = new ResourceForm();
			ResourcePanel.Show(dockPanel, DockState.DockLeft);
			ResourcePanel.RebuildResourceTree();


	
			Video.Init();
		}



		#region Menus envents

		/// <summary>
		/// Website
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void WebSiteMenu_Click(object sender, EventArgs e)
		{
			System.Diagnostics.Process.Start("http://www.codeplex.com/ArcEngine");

		}


		/// <summary>
		/// Collapse resource tree
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void CollapseTreeMenu_Click(object sender, EventArgs e)
		{
			ResourcePanel.CollapseTree();
		}
	
		
		/// <summary>
		/// Quit the application
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
		}


		//
		// About - About
		//
		private void MenuAboutAbout_Click(object sender, EventArgs e)
		{
			AboutForm about = new AboutForm();
			about.ShowDialog();
		}


		/// <summary>
		/// Find level in Amiga memory
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void FindLevelButton_Click(object sender, EventArgs e)
		{
			Wizards.FindMemoryLevelWizard wzd = new Wizards.FindMemoryLevelWizard();
			wzd.ShowDialog();
		}



		/// <summary>
		///  Adds resource to the manager
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void MenuTool_AddExistingResource_Click(object sender, EventArgs e)
		{
			OpenFileDialog dlg = new OpenFileDialog();
			dlg.Filter = "Resource bank (*.bnk)|*.bnk|All Files (*.*)|*.*";
			dlg.Title = "Select resource to open...";
			dlg.DefaultExt = ".bnk";

			DialogResult res = dlg.ShowDialog();
			if (res != DialogResult.OK)
				return;

			// Stay back in the good directory
			Environment.CurrentDirectory = Application.StartupPath;


			ResourceManager.LoadBank(dlg.FileName);

			ResourcePanel.RebuildResourceTree();

		}


		/// <summary>
		/// Closes the selected TabPage from the menu File/Close
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void CloseCurrentTab_OnClick(object sender, EventArgs e)
		{
			
		}


		/// <summary>
		/// Closes all open TabPage 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void MenuCloseAllTabs_OnClick(object sender, EventArgs e)
		{
			
		}



		/// <summary>
		/// Saves the resource under a new name from menu File/Save As
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void FileSaveAs_OnClick(object sender, EventArgs e)
		{
			SaveFileDialog dlg = new SaveFileDialog();
			dlg.Filter = "Resource bank (*.bnk)|*.bnk|All Files (*.*)|*.*";
			dlg.Title = "Save resource as...";
			dlg.DefaultExt = ".bnk";
			dlg.RestoreDirectory = true;
			dlg.AddExtension = true;
			dlg.CheckPathExists = true;
			dlg.OverwritePrompt = true;
			dlg.ValidateNames = true;

			DialogResult res = dlg.ShowDialog();
			if (res != DialogResult.OK)
				return;

			bankName = dlg.FileName;

			ResourceManager.SaveResources(bankName);

			ResourcePanel.RebuildResourceTree();

		}


		#endregion
		

		#region Form Events


		/// <summary>
		/// Expand resource tree 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ExpandTreeMenu_Click(object sender, EventArgs e)
		{
			ResourcePanel.ExpandTree();
		}





		 /// <summary>
		 /// Load resources from a file
		 /// </summary>
		 /// <param name="sender"></param>
		 /// <param name="e"></param>
		 private void OpenBank(object sender, EventArgs e)
		 {
			 OpenFileDialog dlg = new OpenFileDialog();
			 dlg.Filter = "Resource bank (*.bnk)|*.bnk|All Files (*.*)|*.*";
			 dlg.Title = "Select resource to open...";
			 dlg.DefaultExt = ".bnk";
			 dlg.RestoreDirectory = true;
			 dlg.CheckFileExists = true;
			 dlg.CheckPathExists = true;
			 dlg.Multiselect = false;
			 

			 DialogResult res = dlg.ShowDialog();
			 if (res != DialogResult.OK)
				 return;

			 // Erases all resources
			 ResourceManager.Clear();
			 Log.Clear();

			 // Load bank
			 bankName = dlg.FileName;
			 ResourceManager.LoadBank(bankName);

			 dlg.Dispose();
			 dlg = null;

			 ResourcePanel.RebuildResourceTree();

			 if (Log.ErrorCount > 0)
			 {
				 ReportLabel.Text = Log.ErrorCount + " Error(s) occured !";
			 }
			 else
				 ReportLabel.Text = "";
		 }


		/// <summary>
		/// Save all resources to a bank
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SaveResourcesToFile(object sender, EventArgs e)
		{
			if (bankName == null || bankName.Length == 0)
			{
				SaveFileDialog dlg = new SaveFileDialog();
				dlg.Filter = "Resource bank (*.bnk)|*.bnk|All Files (*.*)|*.*";
				dlg.Title = "Save resource as...";
				dlg.DefaultExt = ".bnk";
				dlg.RestoreDirectory = true;
				dlg.AddExtension = true;
				dlg.CheckPathExists = true;
				dlg.OverwritePrompt = true;
				dlg.ValidateNames = true;

				DialogResult res = dlg.ShowDialog();
				if (res != DialogResult.OK)
					return;

				bankName = dlg.FileName;
			}


			ResourceManager.SaveResources(bankName);

			//RebuildResourceTree();
		}




		/// <summary>
		/// Creates an empty resource bank
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void CreateNewBank(object sender, EventArgs e)
		{
			DialogResult res = MessageBox.Show("Erase all resources ?", "", MessageBoxButtons.YesNo);

			if (res != DialogResult.Yes)
				return;
			ResourceManager.Clear();

			// New bank name
			bankName = "";

			//
			ResourcePanel.RebuildResourceTree();
		}



		/// <summary>
		/// Shows preference dialog box
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void PreferenceMenu_Click(object sender, EventArgs e)
		{
			PreferencesForm pref = new PreferencesForm();
			if (pref.ShowDialog() == DialogResult.Cancel)
				return;

		}


		/// <summary>
		/// Adds a binary to the bank
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddBinary_OnClick(object sender, EventArgs e)
		{
			OpenFileDialog dlg = new OpenFileDialog();
			dlg.Filter = "All Files (*.*)|*.*";
			dlg.Title = "Select a binary file...";
			dlg.DefaultExt = ".*";
			dlg.Multiselect = true;
			dlg.RestoreDirectory = true;

			DialogResult res = dlg.ShowDialog();
			if (res != DialogResult.OK)
				return;

			// for each selected file, add it to the bank file
			for (int i=0; i< dlg.FileNames.Length; i++ )
			{
				Binary bin = ResourceManager.CreateBinary(dlg.SafeFileNames[i]);
				if (bin == null)
					continue;

				FileStream stream = File.OpenRead(dlg.FileNames[i]);
				bin.Data = new byte[stream.Length];
				stream.Read(bin.Data, 0, (int)stream.Length);
			}

			ResourcePanel.RebuildResourceTree();

		}


		#endregion


		#region Add new resource

		/// <summary>
		/// Adds a binary
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddBinaryMenu_Click(object sender, EventArgs e)
		{
			OpenFileDialog dlg = new OpenFileDialog();
			dlg.Filter = "All Files (*.*)|*.*";
			dlg.Title = "Select a binary file...";
			dlg.DefaultExt = ".*";
			dlg.Multiselect = true;
			dlg.RestoreDirectory = true;

			DialogResult res = dlg.ShowDialog();
			if (res != DialogResult.OK)
				return;

			// for each selected file, add it to the bank file
			for (int i=0; i< dlg.FileNames.Length; i++)
			{
				Binary bin = ResourceManager.CreateBinary(dlg.SafeFileNames[i]);
				if (bin == null)
					continue;

				FileStream stream = File.OpenRead(dlg.FileNames[i]);
				bin.Data = new byte[stream.Length];
				stream.Read(bin.Data, 0, (int)stream.Length);
			}

			ResourcePanel.RebuildResourceTree();
		}

		/// <summary>
		/// Adds a new Font
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddTTFFontMenu_Click(object sender, EventArgs e)
		{
			Wizards.NewTTFFontWizard wizard = new Wizards.NewTTFFontWizard();
			wizard.ShowDialog();
			ResourcePanel.RebuildResourceTree();
		}

		/// <summary>
		/// Adds a new TileSet
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddTileSetMenu_Click(object sender, EventArgs e)
		{
			Wizards.NewTileSetWizard wizard = new Wizards.NewTileSetWizard();
			wizard.ShowDialog();
			ResourcePanel.RebuildResourceTree();
		}

		/// <summary>
		/// Adds a new level
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddLevelMenu_Click(object sender, EventArgs e)
		{
			Wizards.NewLevelWizard wizard = new Wizards.NewLevelWizard();
			wizard.ShowDialog();
			ResourcePanel.RebuildResourceTree();
		}

		/// <summary>
		/// Adds a new model
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddModelMenu_Click(object sender, EventArgs e)
		{
			Wizards.NewModelWizard wizard = new Wizards.NewModelWizard();
			wizard.ShowDialog();
			ResourcePanel.RebuildResourceTree();
		}

        /// <summary>
        /// Adds a new script
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddScriptMenu_Click(object sender, EventArgs e)
        {
            Wizards.NewScriptWizard wizard = new Wizards.NewScriptWizard();
			wizard.ShowDialog();
			ResourcePanel.RebuildResourceTree();
		}

		/// <summary>
		/// Adds a new animtion
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddAnimationMenu_Click(object sender, EventArgs e)
		{
			Wizards.NewAnimationWizard wizard = new Wizards.NewAnimationWizard();
			wizard.ShowDialog();
			ResourcePanel.RebuildResourceTree();
		}


        /// <summary>
        /// Adds a new Layout
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddLayoutMenu_Click(object sender, EventArgs e)
        {
            Wizards.NewLayoutWizard wizard = new Wizards.NewLayoutWizard();
			wizard.ShowDialog();
			ResourcePanel.RebuildResourceTree();
		}

        /// <summary>
		/// Adds a new String table
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddStringMenu_Click(object sender, EventArgs e)
		{
			Wizards.NewStringTableWizard wizard = new Wizards.NewStringTableWizard();
			wizard.ShowDialog();
			ResourcePanel.RebuildResourceTree();
		}

		/// <summary>
		/// Adds a new texture
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddTextureMenu_Click(object sender, EventArgs e)
		{
			Wizards.NewTextureWizard wizard = new Wizards.NewTextureWizard();
			wizard.ShowDialog();
			ResourcePanel.RebuildResourceTree();
		}

		/// <summary>
		/// Adds a bank to the resourcemanager
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void InsertBankMenu_Click(object sender, EventArgs e)
		{
			OpenFileDialog dlg = new OpenFileDialog();
			dlg.Filter = "Resource bank (*.bnk)|*.bnk|All Files (*.*)|*.*";
			dlg.Title = "Select resource to open...";
			dlg.DefaultExt = ".bnk";

			if (dlg.ShowDialog() != DialogResult.OK)
				return;

			ResourceManager.LoadBank(dlg.FileName);

			ResourcePanel.RebuildResourceTree();
		}


		/// <summary>
		/// Imports a level from a picture
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ImportLevelMenu_Click(object sender, EventArgs e)
		{
			Wizards.ImportLevelWizard wizard = new Wizards.ImportLevelWizard();
			wizard.ShowDialog();
			ResourcePanel.RebuildResourceTree();
		}



		/// <summary>
		/// Close the bank
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void MenuFileClose_Click(object sender, EventArgs e)
		{

		}


		/// <summary>
		/// Import Animatioin From A Gif Image
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void importAnimationFromGifToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Wizards.ImportGIFAnimationWizard wizard = new Wizards.ImportGIFAnimationWizard();
			wizard.ShowDialog();
			ResourcePanel.RebuildResourceTree();
		}

		#endregion


		#region Properties


		/// <summary>
		/// Resource panel
		/// </summary>
		ResourceForm ResourcePanel;

		/// <summary>
		/// Log panel
		/// </summary>
		LogForm logPanel;


		/// <summary>
		/// Name of the opened bank
		/// </summary>
		public string BankName
		{
			get
			{
				return bankName;
			}
		}
		string bankName;


		#endregion
    }
}